<template>
    <div class="code-old">
        <el-form :inline = "true" :model="formInline">
        <el-form-item>
            <el-input v-model="formInline.area" placeholder="地区"></el-input>
        </el-form-item>
        <el-form-item>
            <el-input v-model="formInline.nickname" placeholder="昵称"></el-input>
        </el-form-item>
            <el-form-item label="手机号码">
                <el-input v-model="formInline.phone" placeholder="手机号码"></el-input>
            </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="search">搜索</el-button>
            <el-button type="primary" @click="reset">重置</el-button>
        </el-form-item>
        </el-form>
        <el-table
        :header-cell-style="{color:'#555555'}"
        class="black"
        :data="tableData"
        stripe
        border
        style="width: 100%">
        <el-table-column
            prop="id"
            label="申请邀请码ID"
            align="center">
        </el-table-column>
        <el-table-column
            prop="username"
            label="账号"
            align="center">
        </el-table-column>
        <el-table-column
            prop="area"
            label="地区"
            align="center">
        </el-table-column>
        <el-table-column
            prop="nickname"
            label="昵称"
            align="center">
        </el-table-column>
        <el-table-column
            prop="wechat"
            label="微信号"
            align="center">
        </el-table-column>
        <el-table-column
            prop="qq"
            label="QQ号"
            align="center">
        </el-table-column>
        <el-table-column
            prop="applyCodeInfo"
            label="来源"
            align="center">
        </el-table-column>
        <el-table-column
            prop="applicationTime"
            label="申请时间"
            align="center">
        </el-table-column>
        <el-table-column
            prop="inviteCode"
            label="邀请码"
            align="center">
        </el-table-column>
        <el-table-column
            prop="isUsed"
            label="状态"
            align="center">
        </el-table-column>
        <el-table-column label="操作" align="center" width = "150">
            <template slot-scope="scope">
            <el-button @click="onBuildCodeClick(scope.row)" size="small">生成邀请码</el-button>
            </template>
        </el-table-column>
        </el-table>
        <div class="footer-page">
            <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="page"
            :page-sizes="[10, 20, 50, 100]"
            :page-size="page_size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total_page">
            </el-pagination>
        </div>
        <el-dialog
        title="提示"
        :visible.sync="dialogVisible"
        width="30%">
        <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="onSureClick">确 定</el-button>
        </span>
        </el-dialog>
    </div>
</template>

<script>
export default {
    name: 'code-old',
    data () {
        return {
            page: 1,
            page_size: 10,
            total_page: 0,
            formInline: {
                area: '',
                nickname: '',
                phone: ''
            },
            params: {
                page: 1,
                limit: 10,
                order: 'asc',
                type: 2
            },
            tableData: [],
            dialogVisible: false,
            state_id: ''
        }
    },
    created () {

    },
    mounted () {
        this.getList({ 'params': this.params })
    },
    methods: {
        getList (data) {
            this.tableData = []
            this.$api.code.getCodeList(data, res => {
                if (res.code === 200) {
                    let data = res.page.list || []
                    for (let i = 0; i < data.length; i++) {
                        this.tableData.push(data[i])
                    }
                    this.total_page = res.page.totalCount
                    this.page_size = res.page.pageSize
                    this.page = res.page.currPage
                }
            })
        },
        search () {
            this.page = 1
            this.params['area'] = this.formInline.area
            this.params['nickname'] = this.formInline.nickname
            this.params['phone'] = this.formInline.phone
            this.getList({ 'params': this.params })
        },
        reset () {
            this.page = 1
            this.formInline.area = ''
            this.formInline.nickname = ''
            this.formInline.phone = ''
            this.params['area'] = this.formInline.area
            this.params['nickname'] = this.formInline.nickname
            this.params['phone'] = this.formInline.phone
            this.getList({ 'params': this.params })
        },
        onBuildCodeClick (row) {
            this.dialogVisible = true
            this.state_id = row.id
        },
        onSureClick () {
            this.$api.code.buildCode(this.state_id, res => {
                if (res.code === 200) {
                    this.$message.success(res.msg)
                    this.dialogVisible = false
                    this.reset()
                } else {
                    this.$message.error(res.msg)
                }
            })
        },
        handleSizeChange (val) {
            this.page_size = val
            this.params.limit = val
            this.getList({ 'params': this.params })
        },
        handleCurrentChange (val) {
            this.page = val
            this.params.page = val
            this.getList({ 'params': this.params })
        }
    }
}
</script>

<style lang="less" scoped>
    .code-old {
        position: relative;
        width: 100%;
        height: 100%;
        .margin-bottom-12 {
            margin-bottom: 10px;
        }
        .el-row {
            margin-bottom: 20px;
            &:last-child {
                margin-bottom: 0;
            }
        }
        .el-col {
            border-radius: 4px;
        }
        .grid-content {
            position: relative;
            height: 40px;
        }
        .grid-padding {
            /* padding-left: 20px; */
            padding-right: 20px;
        }
        .grid-title {
            float: left;
            width: 72px;
            height: 40px;
            line-height: 40px;
            font-size: 14px;
            color: #333;
            text-align: center;
        }
        .grid-input {
            float: left;
            width: 130px;
            height: 40px;
            line-height: 40px;
            font-size: 14px;
            color: #333;
            text-align: center;
        }
        .row-bg {
            padding: 10px 0;
            background-color: #f9fafc;
        }
        .footer-page {
            width: 540px;
            margin: 0 auto;
            padding-top: 20px;
        }
    }
</style>
